<template>
  <n-drawer v-model:show="isDrawer" :width="width" :placement="placement">
    <n-drawer-content :title="formParams.id ? '编辑角色':'添加角色'" closable>
      <n-form
        :model="formParams"
        :rules="rules"
        ref="formRef"
        label-placement="left"
        :label-width="100"
      >
        <n-form-item label="角色名" path="name">
          <n-input placeholder="请输入角色名" v-model:value="formParams.name" />
        </n-form-item>
        <n-form-item label="角色说明" path="explain">
          <n-input placeholder="请输入角色说明" v-model:value="formParams.explain" />
        </n-form-item>
        <n-form-item label="是否启用" path="status">
          <n-radio-group v-model:value="formParams.status" name="status">
            <n-space>
              <n-radio value="enable">是</n-radio>
              <n-radio value="disable">否</n-radio>
            </n-space>
          </n-radio-group>
        </n-form-item>
        <n-form-item label="默认角色" path="isDefault">
          <n-switch v-model:value="formParams.isDefault" />
        </n-form-item>
      </n-form>

      <template #footer>
        <n-space>
          <n-button type="primary" :loading="subLoading" @click="formSubmit">提交</n-button>
          <n-button @click="handleReset">重置</n-button>
        </n-space>
      </template>
    </n-drawer-content>
  </n-drawer>
</template>

<script lang="ts">
  import { defineComponent, reactive, ref, toRefs, inject } from 'vue';
  import { useMessage } from 'naive-ui';
  import {addRole, editRole} from '@/api/system/role';

  const rules = {
    name: {
      required: true,
      message: '请输入角色名',
      trigger: 'blur',
    },
  };
  export default defineComponent({
    name: 'CreateDrawer',
    components: {},
    props: {
      width: {
        type: Number,
        default: 450,
      },
    },
    setup() {
      const message = useMessage();
      const formRef: any = ref(null);
      const reloadPage = inject('reloadPage') as Function;

      const defaultValueRef = () => ({
        id: undefined,
        name: '',
        explain: '',
        isDefault: 0,
        status: false,
      });

      const state = reactive({
        isDrawer: false,
        subLoading: false,
        formParams: defaultValueRef(),
        placement: 'right' as const,
        alertText:
          '该功能主要实时预览各种布局效果，更多完整配置在 projectSetting.ts 中设置，建议在生产环境关闭该布局预览功能。',
      });

      function openDrawer(isInit: boolean=false) {
        if (isInit) {
          state.formParams = Object.assign(state.formParams, defaultValueRef());
        }
        state.isDrawer = true;
      }

      function closeDrawer() {
        state.isDrawer = false;
      }

      function formSubmit() {
        formRef.value.validate(async (errors) => {
          if (!errors) {
            let res = formRef.value.model.id ? await editRole({ ...formRef.value.model, })
              : await addRole({ ...formRef.value.model, });
            if (res.code === 200) {
              message.success(res.message);
              handleReset();
            } else {
              message.error(res.message);
            }
            closeDrawer();
          } else {
            message.error('请填写完整信息');
          }
        });
      }

      function handleReset() {
        formRef.value.restoreValidation();
        state.formParams = Object.assign(state.formParams, defaultValueRef());
        reloadPage();
      }

      return {
        ...toRefs(state),
        formRef,
        rules,
        formSubmit,
        handleReset,
        openDrawer,
        closeDrawer,
      };
    },
  });
</script>
